import TableData from "@/components/public/el-table"; //主页面列表
import TableLyout from '@/components/commonPublic/tableLyout.vue'
import {
    getLabInfoByCompanyId, //试验室基本信息
    getTesterLists, //检测人员信息
    getLabEquipLists, //仪器设备信息
} from "@/api/request.js";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
// import publicForm from "@/components/public/el-form.vue"; //弹框form

import formJs from "./regexDetail.js"; //新增弹框配置
import actions from "@/actions";
import API from '@/utils/commonRequest.js'


export default {
    name: "filing",
    data() {
        return {
            name: "filing",
            typeData: [
                { text: "试验室基本信息", show: true },
                { text: "检测人员信息", show: false },
                { text: "仪器设备信息", show: false },
            ],
            userNature: null, //用户Nature

            projectsObj: [], //项目名
            tendersObj: [],
            isLock: false,
            loading: false,

            getList: {
                fprojectid: "",
                fCompanyid: "", //左侧选中组织机构id
                page: 1, //当前页码
                pageSize: 15, //每页显示条数
                userId: "", //查询待办或已办时传 列表页查询不传
                modelType: 2, //0-待办 1-已办 传2 表示列表页查询
                keyWord: "",//模糊字段查询
                keyType: "", // 样品类型
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                timeDates: [],
            },
            // 试验室基本信息
            searchObj_laboratory: formJs.searchObj_laboratory, //搜索条件
            formObj: formJs.formObj,
            formData: formJs.formData,
            fSourcefileList: [], //资料
            // 试验室基本信息

            // 检测人员信息
            btnObj: formJs.btnObj, //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            tableData: [],
            // 检测人员信息

            // 仪器设备信息
            btnObj_device: formJs.btnObj_device, //按钮
            queryCriteriaData_device: {}, //分页
            searchObj_device: formJs.searchObj_device, //搜索条件
            table_config_device: formJs.table_config_device, //列表
            dataFid_device: null, //列表详情id
            currentRow_device: null, //列表详情
            tableData_device: [],
            // 仪器设备信息
        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
    },
    mounted() {
        this.typeData = [
            { text: "试验室基本信息", show: true },
            { text: "检测人员信息", show: false },
            { text: "仪器设备信息", show: false },
        ];
        API.userNature().then((res) => {
            if (res.errcode == 0) {
                this.userNature = res.data.Nature;
            }
        }).catch((err) => {
            console.log("API", err)
        })
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.projectsObj = data;
            this.userInfo = prev.currentUser;
        }, true);

        this.getDate();
    },
    destroyed() {
    },
    methods: {
        changeType(e, index) {
            for (let i in this.typeData) {
                if (parseFloat(i) === index) {
                    this.typeData[i].show = true;
                } else {
                    this.typeData[i].show = false;
                }
            }
            this.indexNum = index;
            this.getList.page = 1;
            this.getList.keyWord = "";
            if (index == 0) {
                this.yj_searchForm(this.getList);
            } else if (index == 1) {
                this.getTesterForm(this.getList);
            } else if (index == 2) {
                this.getLabEquipForm(this.getList);
            }
        },

        reset(e) {
            this.getList = this.$options.data.call(this).getList;
            this.getList.fprojectid = e;
        },
        getBtnName(name, status, num) {
            this.yj_edit(status, num);
        },

        // 新增/编辑
        yj_edit(status, num) {
            var data;
            var that = this;
            if (num == 1) {
                if (that.userNature == '施工单位') {
                    if (status == 5) {//导出
                        that.$message.error({
                            content: "导出功能暂未开放",
                            duration: 1000
                        });
                    }
                } else {
                    that.$message.error({
                        content: "该用户无法操作",
                        duration: 1000
                    });
                }

                if (status == 7) { //查看
                    if (that.dataFid != null) {
                        that.$router.push({
                            path: "/filingTestPersonnelInfo",
                        });
                        data = {
                            data: that.currentRow,
                            getList: that.getList,
                            status: status,
                        }
                    } else {
                        that.$message.warning({
                            content: "请先选择需要查看的数据",
                            duration: 1000
                        })
                    }

                // sessionStorage.setItem("GxSampleManage_filingEquipmentInfo", JSON.stringify(data));

                }
               
            } else if (num == 2) {
                if (that.userNature == '施工单位') {
                    if (status == 5) {//导出
                        that.$message.error({
                            content: "导出功能暂未开放",
                            duration: 1000
                        });
                    }
                } else {
                    that.$message.error({
                        content: "该用户无法操作",
                        duration: 1000
                    });
                }

                if (status == 7) { //查看
                   
                    if (that.dataFid_device != null) {
                        that.$router.push({
                            path: "/filingEquipmentInfo",
                        });
                        data = {
                            data: that.currentRow_device,
                            getList: that.getList,
                            status: status,
                        }
                        console.log(8888,data);
                    } else {
                        that.$message.warning({
                            content: "请先选择需要查看的数据",
                            duration: 1000
                        });

                    }

                    // 
                }
            }
            sessionStorage.setItem("GxSampleManage_filingTestPersonnelInfo", JSON.stringify(data));

        },


        // 查看
        // 点击查看
        yj_viewForm(row, label) {
            this.currentRow = row;
            this.dataFid = row.fId;
            if (label == "姓名") {
                this.yj_edit(7, 1);
            }
        },
        yj_viewForm_device(row, label) {
            this.currentRow_device = row;
            this.dataFid_device = row.fId;
            if (label == "设备编号") {
                this.yj_edit(7, 2);
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val;
            this.dataFid = val.fId;
        },
        rowChange_device(val) {
            this.currentRow_device = val;
            this.dataFid_device = val.fId;
        },

        // 获取列表数据
        //试验室基本信息
        yj_searchForm(data) {
            this.loading = true;
            window.sessionStorage.setItem(
                "GxEmbedded-testDetectionManage_getList",
                JSON.stringify(data)
            );
            var id;
            if (data.fCompanyid != "") {
                id = data.fCompanyid;
            } else {
                id = data.fprojectid;
            }
            getLabInfoByCompanyId({
                companyId: id,
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        if (res.data.labbasicinfo == null) {
                            this.formData = {
                                fId: "", // 信息fId
                                fCompanyid: "", // 所属标段ID
                                fCompanyname: "", // 所属标段名称
                                fJlid: "", // 所属监理ID
                                fJlname: "", // 所属监理名称
                                fProjectid: "", // 所属项目ID
                                fProjectname: "", // 所属项目名称
                                fLabname: "", // 实验室名称
                                fLabshortname: "", // 实验室简称
                                fAuthorizedagencyid: "", // 授权机构ID
                                fAuthorizedagencyname: "", // 授权机构名称
                                fAuthorizedagencycertificatetype: "", // 授权机构登记证书
                                fAuthorizedagencycertificateno: "", // 授权机构证书编号
                                fBuilddates: "", // 实验室设立日期
                                fEngineercount: "", // 试验检测工程师人数（人）
                                fTestercount: "", // 试验检测员人数（人）
                                fBuildingarea: "", // 试验检测用房总面积
                                fAuthorizedsuperintendentid: "", // 授权负责人ID
                                fAuthorizedsuperintendentname: "", // 授权负责人名称
                                fAuthorizedsuperintendentcertificateno: "", // 授权负责人持证书号
                                fAuthorizedsuperintendenttitle: "", // 授权负责人职称
                                fAuthorizedsuperintendenttel: "", // 授权负责人联系电话
                                fLabaddress: "", // 工地实验室地址
                                fPostalcode: "", // 邮编
                                fLabtel: "", // 办公联系电话
                                fFile: "", // 附件资料ID
                                fAuthorizedscope: "", // 授权业务范围
                                fileList: [],
                            };
                        } else {
                            this.formData = res.data.labbasicinfo;
                        }
                        this.fSourcefileList = this.formData.fileList;
                        this.loading = false;
                    } else {
                        this.$message.error({
                            content: "试验室基本信息" + res.msg,
                            duration: 1000
                        })
                        this.loading = false;
                    }
                })
                .catch(() => {
                    this.loading = false;
                });
        },
        //检测人员信息
        getTesterForm(data) {
            window.sessionStorage.setItem(
                "GxEmbedded-testDetectionManage_getList",
                JSON.stringify(data)
            );
            var id;
            if (data.fCompanyid != "") {
                id = data.fCompanyid;
            } else {
                id = data.fprojectid;
            }
            this.loading = true;
            getTesterLists({
                companyId: id, // 左侧选中组织机构id
                keyWord: data.keyWord,
                page: data.page, // 当前页码
                pageSize: 15, // 每页显示条数
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.tableData = res.data.testerList.list;
                        that.queryCriteriaData = res.data.testerList;

                    } else {
                        this.$message.error({
                            content: "检测人员信息" + res.msg,
                            duration: 1000
                        });
                        this.loading = false;
                    }
                })
                .catch(() => {
                    this.loading = false;
                });
        },
        //仪器设备信息
        getLabEquipForm(data) {
            window.sessionStorage.setItem(
                "GxEmbedded-testDetectionManage_getList",
                JSON.stringify(data)
            );
            var id;
            if (data.fCompanyid != "") {
                id = data.fCompanyid;
            } else {
                id = data.fprojectid;
            }
            this.loading = true;
            getLabEquipLists({
                companyId: id, // 左侧选中组织机构id
                keyWord: data.keyWord,
                page: data.page, // 当前页码
                pageSize: 15, // 每页显示条数
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.tableData_device = res.data.labEquipLists.list;
                        that.queryCriteriaData_device = res.data.labEquipLists;
                        // that.navigatepageNums1 =
                        //     that.queryCriteriaData1.navigatepageNums.length;
                    } else {
                        this.$message.error({
                            content: "仪器设备信息" + res.msg,
                            duration: 1000
                        });
                        this.loading = false;
                    }
                })
                .catch(() => {
                    this.loading = false;
                });
        },
        // 获取数据

        getDate() {
            if (JSON.parse(sessionStorage.getItem("GxEmbedded-testDetectionManage_getList"))) {
                var data = JSON.parse(sessionStorage.getItem("GxEmbedded-testDetectionManage_getList"));
                if (data.fprojectid != "") {
                    this.getTender(data.fprojectid);
                } else {
                    data.fprojectid = this.projectsObj[1].fItemValue;
                    this.getTender(data.fprojectid);
                }
                if (data.startDates == "" || data.startDates == undefined || data.startDates == null || data.endDates == "" || data.endDates == undefined || data.endDates == null) {
                    data.startDates = this.common.getDay(-90); // 开始时间
                    data.endDates = this.common.getDay(0); // 截至时间
                }
                this.getList = {
                    fprojectid: data.fprojectid,
                    fCompanyid: data.fCompanyid, //左侧选中组织机构id
                    page: 1, //当前页码
                    pageSize: 15, //每页显示条数
                    userId: this.userInfo.id, //查询待办或已办时传 列表页查询不传
                    modelType: "",
                    keyWord: "",
                    keyType: "", // 样品类型
                    startDates: data.startDates, // 开始时间
                    endDates: data.endDates, // 截至时间
                    timeDates: [data.startDates, data.endDates],
                };

                console.log("this.getList", this.getList);
            } else {
                this.getList.startDates = this.common.getDay(-90); // 开始时间
                this.getList.endDates = this.common.getDay(0); // 截至时间
                this.getList.timeDates = [
                    this.getList.startDates,
                    this.getList.endDates,
                ];
            }
        },

        // 筛选条件
        // 标段查询
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: localStorage.getItem("access_token"),
                },
            }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.tendersObj = [];
                    if (data.length == 0) {
                        this.$message.warning({
                            content: "有其他人员登录此账号！",
                            duration: 1000,
                        });
                        setTimeout(() => {
                            window.location.href = "http://172.18.5.201:1080/login";
                        }, 1200);
                    } else    if (data.length == 1) {
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                        this.getList.fCompanyid = data[0].sectionId;
                    } else {
                        this.tendersObj = [{
                            fItemName: "所有标段",
                            fItemValue: "",
                        },];
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                    }
                }
            });
        },
        // 标段变化
        selectCompanyid(e, index, prop) {
            console.log("selectCompanyid", e, index, prop);
        },
        selectCompanyid_device(e, index, prop) {
            console.log("selectCompanyid_device", e, index, prop);
        },
        // 筛选条件
    },
};